package com.starry.home.ui.frament

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.starry.core.base.BaseFragment
import com.starry.core.constant.GlobalConfig
import com.starry.core.ext.singleClick
import com.starry.core.ext.toast
import com.starry.core.utils.SpUtil
import com.starry.home.databinding.FragmentMineBinding
import com.starry.home.logic.model.login.UserInfoBean
import com.starry.home.ui.login.LoginActivity
import com.starry.home.ui.main.MedicalActivity
import com.starry.home.ui.main.PreviewActivity
import com.starry.home.ui.mine.HealthRecordActivity
import com.starry.home.ui.mine.MyDoctorActivity
import com.starry.home.ui.mine.UserInfoActivity
import com.starry.home.viewmodel.main.MainViewModel

/**
 * Package:com.starry.dotpen_patients.ui.main
 * Author:starr
 * Time:2024/1/4 15:14
 * Description: 我的界面
 */
class MineFragment : BaseFragment<FragmentMineBinding, MainViewModel>() {


    private lateinit var userInfo: UserInfoBean

    companion object {
        private const val PARAM = "param"
        fun newInstance(param: String?): MineFragment {
            val fragment = MineFragment()
            val args = Bundle()
            args.putString(PARAM, param)
            fragment.arguments = args
            return fragment
        }
    }

    override fun createObserver() {

    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun initData() {


        mDataBinding.tvChat.singleClick {
            ARouter.getInstance().build("/chat/ui/ChatActivity").navigation()
        }

        mViewModel.userInfo.observe(this) {

            if (it.isSuccess) {
                if (it.getOrNull() == null || it.getOrNull()?.emrHzInfo == null) {
                    return@observe
                }
                userInfo = it.getOrNull()!!.emrHzInfo
                SpUtil.setStringValue(GlobalConfig.userName, userInfo.emrHzName)
                SpUtil.setStringValue(GlobalConfig.userImg, userInfo.emrHzImg)
                Glide.with(requireActivity()).load(SpUtil.getStringValue(GlobalConfig.userImg))
                    .into(mDataBinding.ivUser)
                mDataBinding.tvUser.text = SpUtil.getStringValue(GlobalConfig.userName)
            } else {
                toast("登录状态失效，请重新登录")
            }
        }

        mDataBinding.ivEditUserInfo.singleClick {
            UserInfoActivity.startAction(mContext)
        }

        mDataBinding.tvMedical.singleClick {
            MedicalActivity.startAction(mContext)
        }

        mDataBinding.tvPreview.singleClick {
            PreviewActivity.startAction(mContext)
        }

        mDataBinding.tvBook.singleClick {
            val bundle = Bundle()
            bundle.putSerializable("userInfo", userInfo)
            HealthRecordActivity.startAction(mContext, bundle)
        }

        mDataBinding.tvDoctor.singleClick {
            MyDoctorActivity.startAction(mContext)
        }
    }

    override fun initView() {
        mDataBinding.click = MineClick()
        val userId = SpUtil.getIntValue(GlobalConfig.userId)
        if (userId == -1) {
            toast("登录状态失效，请重新登录")
            mDataBinding.exit.text = "前往登录"
        }
        mViewModel.getUserInfo(userId)
        mDataBinding.titleTopBar.baseTvTitle.text = "我的"
        mDataBinding.titleTopBar.baseIvBack.visibility = View.GONE
    }


    inner class MineClick {
        fun exit(view: View?) {
            SpUtil.clear()
            val bundle = Bundle()
            bundle.putBoolean("isLogin", true)
            LoginActivity.startAction(mContext, bundle)
            this@MineFragment.requireActivity().finish()
        }
    }
}